package com.free.fs.controller;

import com.free.fs.common.domain.R;
import com.free.fs.model.DocPojo;
import com.free.fs.service.PublicDocService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 公共文档访问控制器（无需登录）
 *
 * @author dinghao
 * @date 2024/01/01
 */
@RestController
@RequestMapping("public/doc")
@RequiredArgsConstructor
public class PublicDocController {

    private final PublicDocService publicDocService;

    /**
     * 获取公共文档列表
     *
     * @param path 目录路径
     * @return
     */
    @GetMapping("/files")
    public R<List<DocPojo>> getDocList(@RequestParam(required = false, defaultValue = "") String path) {
        List<DocPojo> docs = publicDocService.getDocList(path);
        return R.succeed(docs, "查询成功");
    }

    /**
     * 下载文档
     *
     * @param docId 文档ID
     * @param response
     */
    @GetMapping("/download/{docId}")
    public void downloadDoc(@PathVariable Long docId, 
                           @RequestParam(required = false) String fileName, 
                           HttpServletResponse response) {
        publicDocService.downloadDoc(docId, fileName, response);
    }

    /**
     * 根据文档路径下载
     *
     * @param docPath 文档路径
     * @param fileName 自定义文件名（可选）
     * @param response
     */
    @GetMapping("/downloadByPath")
    public void downloadByPath(@RequestParam String docPath, 
                              @RequestParam(required = false) String fileName, 
                              HttpServletResponse response) {
        publicDocService.downloadDocByPath(docPath, fileName, response);
    }

    /**
     * 获取文档分类列表
     *
     * @return
     */
    @GetMapping("/categories")
    public R<List<DocPojo>> getDocCategories() {
        List<DocPojo> categories = publicDocService.getDocCategories();
        return R.succeed(categories, "获取成功");
    }
}